/*
 * @Author: your name
 * @Date: 2021-11-10 15:14:51
 * @LastEditTime: 2021-11-10 15:16:09
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\middleware\auth.js
 */
module.exports = () => {
    return async function auth(ctx, next) {
        // 取出来authorization
        // 截取出来 token
        // 校验token
        const { authorization } = ctx.request.header;
        if (authorization) {
            try {
                const token = authorization.split(" ")[1];
                const jwt = require("jsonwebtoken");
                jwt.verify(token, ctx.app.config.keys);
                await next();
            } catch (err) {
                ctx.status = 401;
                ctx.body = {
                    success: false,
                    message: "解析失败" + err.message,
                };
            }
        } else {
            ctx.status = 401;
            ctx.body = {
                success: false,
                message: "请先登录",
            };
        }
    };
};